<?php
//需要在php.ini中打开extension=mbstring
//需要在php.ini中打开extension=curl
//发送请求需要下载和使用最新的证书https://curl.se/docs/caextract.html

$AppId     = "xxxxxxxx";        #APPID
$AppSecret = "xxxxxxxx";        #密钥
$ApiUrl    = "https://iot-app-prod.fengdada.cn/mk/api"; #API地址
$PrinterId = "xxxxxxxx";                         #打印机ID

function encode($BizData, $nonce) {
  global  $AppSecret;
  $jsonBytes = mb_convert_encoding($BizData , 'utf-8');
  $bizData = strval($jsonBytes);
  $sign_ori = $bizData . $nonce . $AppSecret;
  $md5_hash = md5($sign_ori, true);
  $sign = base64_encode($md5_hash);
  return $sign;
}

function generate_verification_code() {
  $verification_code = "";
  for ($i = 0; $i < 6; $i++) {
      $verification_code .= strval(rand(0, 9));
  }
  return $verification_code;
}

function requests_post($url, $data, $headers) {
  $ch = curl_init();
 // curl_setopt($ch, CURLOPT_CAINFO, "cacert-2023-01-10.pem");
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  return $response;
}


function DoMyPost($URL, $json_BizData) {
  global  $AppId;
  $ts = round(microtime(true) * 1000);
  $nonce = generate_verification_code();
  $sign = encode($json_BizData, $nonce);
  
  $data = array(
      "bizData" => $json_BizData,
      "nonce" => $nonce,
      "appId" => $AppId,
      "timestamp" => strval($ts)
  );

  $headers = array(
    'Content-Type: application/json',
    'sign:'.$sign
  );

  $response = requests_post($URL, json_encode($data), $headers);
  if(empty($response)) { 
    echo "Error: no response received"; 
  }else{
    echo  $response;
  }
}

 // 云打印验证码 
 function PrintCaptcha($PrinterId) { 
  global  $ApiUrl;
  $URL = $ApiUrl."/print/captcha"; 
  $BizData = array('printerId' => $PrinterId); 
  $json_BizData = json_encode($BizData); 
  DoMyPost($URL, $json_BizData); 
} 
// 云打印机绑定 
function PrintBind($PrinterId, $VerificationCode) { 
  global  $ApiUrl;
  $URL = $ApiUrl."/printer/bind"; 
  $BizData = array('printerId' => $PrinterId, 'captcha' => $VerificationCode); 
  $json_BizData = json_encode($BizData); DoMyPost($URL, $json_BizData); 
} 
// 云打印 
function CloudPrint($PrinterId, $ShareCode, $PrintDataList) { 
  global  $ApiUrl;
  $URL = $ApiUrl."/print"; 
  $BizData = array('printerId' => $PrinterId, 'shareCode' => $ShareCode, 'printData' => $PrintDataList); 
  $json_BizData = json_encode($BizData); 
  DoMyPost($URL, $json_BizData); 
} 
// 云打印状态查询 
function QueryPrintStatus($PrinterId, $ShareCode) { 
  global  $ApiUrl;
  $URL = $ApiUrl."/printer/status/query"; 
  $BizData = array('printerId' => $PrinterId, 'shareCode' => $ShareCode); 
  $json_BizData = json_encode($BizData); 
  DoMyPost($URL, $json_BizData); 
} 

//------------------------入口------------------------
// 1. 获取验证码
// PrintCaptcha($PrinterId);

// 2. 绑定打印机
//$VerificationCode = "376838";
//PrintBind($PrinterId, $VerificationCode);

// 3. 云打印
$ShareCode = "xxxxxx";
$TemplateId = "xxx";
$waybillPrinterData = array("receiveManAddress"=>"塘苗路18号",
"orderId"=>"10256485649",
"needFetch"=>"true",
"packageWeight"=>"",
"companyName"=>"",
"sendProvince"=>"浙江省",
"errorCode"=>"",
"receiverProvince"=>"浙江省",
"receiverCity"=>"杭州市",
"senderMan"=>"张三",
"senderArea"=>"西湖区",
"receiveMan"=>"李四",
"postStationId"=>"",
"itemName"=>"",
"specialType"=>"代收货款",
"senderPostcode"=>"",
"payMethod"=>"0",
"id"=>"1933",
"logisticCode"=>"BestSmart10256485649",
"machineCode"=>"1843923524226238",
"errorDesc"=>"",
"balanceStatus"=>"",
"specialMoney"=>" 39 元",
"expressType"=>"0",
"sendCounty"=>"西湖区",
"weight"=>"",
"receiverPostcode"=>"",
"version"=>"1",
"senderCity"=>"杭州市",
"receiveProvince"=>"浙江省",
"printResult"=>"NONE",
"sendMan"=>"张三",
"companyId"=>"",
"expressCost"=>"",
"receiverManPhone"=>"18598765432",
"mainId"=>"",
"senderManPhone"=>"15012345678",
"remark1"=>"",
"remark3"=>"",
"receiveCounty"=>"西湖区",
"remark2"=>"",
"lastUpdateTime"=>"Wed Jul 29 13=>53=>06 CST 2015",
"markDestination"=>"测-测试单(2)(浙-2)",
"sendCity"=>"杭州市",
"pkgCode"=>"测试测试包",
"receiverMan"=>"李四",
"errorInfo"=>"null",
"remark"=>"【批次1/1】备注",
"sendManPhone"=>"15012345678",
"batchId"=>"1/1",
"pickBy"=>"",
"result"=>"SUCCESS",
"mailNo"=>"12345678901234",
"shippingStatus"=>"NOMESSAGE",
"serialVersionUID"=>"-1229572053273904313",
"richItemInformation"=>"",
"receiveManPhone"=>"18598765432",
"udf7"=>"",
"billProvideSiteCode"=>"200000",
"logisticsProviderCode"=>"HTKY",
"udf8"=>"",
"printCount"=>"1",
"senderManAddress"=>"教工路1号",
"custId"=>"null",
"itemWeight"=>"",
"receiverManAddress"=>"塘苗路18号",
"isReprint"=>"false",
"orderSource"=>"BestSmart",
"udf5"=>"测试",
"udf6"=>"",
"senderProvince"=>"浙江省",
"receiverArea"=>"西湖区",
"udf3"=>"",
"udf4"=>"",
"udf1"=>"",
"userId"=>"",
"udf2"=>"",
"postStationName"=>"",
"itemCount"=>"0",
"logisticProviderID"=>"HTKY",
"billProvideSiteName"=>"上海分拨中心",
"createTime"=>"Wed Jul 29 13=>37=>40 CST 2015",
"receiveCity"=>"杭州市",
"sendManAddress"=>"教工路1号",
"printTime"=>"",
"username"=>"");
$printDataList = array(array("waybillPrinterData" => $waybillPrinterData, "templateId" => $TemplateId));
$PData = $printDataList;
//CloudPrint($PrinterId, $ShareCode, $PData);

// 4. 云打印机状态查询
 QueryPrintStatus($PrinterId, $ShareCode);
?>